package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.NoteComment;
import cn.wolfcode.wolf2w.business.mapper.NoteCommentMapper;
import cn.wolfcode.wolf2w.business.query.NoteCommentQuery;
import cn.wolfcode.wolf2w.business.service.INoteCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 游记评论Service业务层处理
 * 
 * @author libai
 * @date 2024-08-02
 */
@Service
@Transactional
public class NoteCommentServiceImpl extends ServiceImpl<NoteCommentMapper,NoteComment> implements INoteCommentService {

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Override
    public IPage<NoteComment> queryPage(NoteCommentQuery qo) {
        IPage<NoteComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        IPage<NoteComment> pageResult = lambdaQuery().page(page);
        for (NoteComment record : pageResult.getRecords()) {
            R<UserInfo> r = remoteUserInfoService.getOne(record.getUserId(), SecurityConstants.INNER);
            if (r.getCode()==200){
                record.setUser(r.getData());
            }

            if (record.getRefId()!=null){
                 NoteComment refComment = getById(record.getRefId());
                final R<UserInfo> one = remoteUserInfoService.getOne(refComment.getUserId(), SecurityConstants.INNER);
                if (one.getCode()==200){
                    refComment.setUser(one.getData());
                }
                record.setRefComment(refComment);
            }
        }
        return pageResult;
    }

    @Override
    public void insert(NoteComment comment) {
        save(comment);
    }
}
